package net.bhzyxy.azyy;

import android.os.Bundle;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatImageView;

import java.util.Objects;

public class WebViewActivity extends AppCompatActivity {

    //定义变量
    private AppCompatEditText urlEditText;
    private AppCompatImageView sendImageView;
    private WebView mainWebView;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置布局文件（绑定布局文件）
        setContentView(R.layout.activity_webview);

        urlEditText = findViewById(R.id.urlEditText);
        sendImageView = findViewById(R.id.sendImageView);
        mainWebView = findViewById(R.id.mainWebView);



        //接收父类传递信息
        String url = getIntent().getStringExtra("url");

        //设置webView
        WebSettings webSettings = mainWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        //缓存设置
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);

        mainWebView.loadUrl(url);

        mainWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                urlEditText.setText(url);
                mainWebView.loadUrl(url);
                return false;
            }
        });

        sendImageView.setOnClickListener(view -> {
            mainWebView.loadUrl(Objects.requireNonNull(urlEditText.getText()).toString());
        });


        Log.d("TAG", "WebActivity:onCreate");

//        //后退
//        leftImageView.setOnClickListener(view -> mainWebView.goBack());
//
//        //刷新
//        replayImageView.setOnClickListener(view -> mainWebView.reload());
//
//        //前进
//        rightImageView.setOnClickListener(view -> mainWebView.goForward());
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("TAG", "WebActivity:onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("TAG", "WebActivity:onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("TAG", "WebActivity:onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("TAG", "WebActivity:onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("TAG", "WebActivity:onDestory");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("TAG", "WebActivity:onRestart");
    }
}
